package com.glsc.ngateway.platform.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.api.ldap.dto.LdapEhrEmpDto;
import com.glsc.ngateway.common.api.platform.dto.user.PlatformUserDto;
import com.glsc.ngateway.common.base.domain.mysql.gateway.ldap.DepartmentVisitSystem;
import com.glsc.ngateway.common.base.domain.mysql.gateway.ldap.UserVisitSystem;
import com.glsc.ngateway.common.base.exception.PlatformException;
import com.glsc.ngateway.common.base.repo.mysql.gateway.ldap.LdapUserSystemRepo;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.it.ItApplicationDetailRepo;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.it.ItOperationRepo;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.it.ItVirtualDeviceRepo;
import com.glsc.ngateway.platform.service.it.SequenceService;
import com.glsc.ngateway.platform.service.it.ServerService;
import com.glsc.ngateway.platform.utils.SpecificationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Optional;

@Service
public class LdapUserVisitService {

    Logger logger = LoggerFactory.getLogger(LdapUserVisitService.class);
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private SequenceService sequenceService;
    @Autowired
    private UserService userService;
    @Autowired
    private ItVirtualDeviceRepo itVirtualDeviceRepo;
    @Autowired
    private ItOperationRepo itOperationRepo;
    @Autowired
    private ItApplicationDetailRepo itApplicationDetailRepo;

    @Autowired
    private ServerService serverService;

    @Autowired
    private MailService mailService;

    @Resource
    private LdapUserSystemRepo ldapUserSystemRepo;


    @Transactional
    public void addOrUpdate(UserVisitSystem systemMember) {
        LdapEhrEmpDto user = userService.getUserFromEhr(systemMember.getUserAccount());
        systemMember.setUserName(user.getName());

//        SystemVendorLogInfo log = new SystemVendorLogInfo();
//        if (StrUtil.isNotEmpty(systemMember.getVendorCode())){
//            //修改
//            log.setLogFlag(DictConstant.UPDATE);
//        } else {
//            //新增
//            log.setLogFlag(DictConstant.ADD);
//        }
//        BeanUtil.copyProperties(systemMember,log);
//        systemMember.setDeleted(DictConstant.NO_DELETE);
        UserVisitSystem db = ldapUserSystemRepo.findOneByUserAccount(systemMember.getUserAccount());
        //更新库里的数据，没有再新增
        if (ObjectUtil.isNotEmpty(db) && systemMember.getId() == null){
            BeanUtil.copyProperties(systemMember,db,"id");
            ldapUserSystemRepo.save(db);
        } else {
            ldapUserSystemRepo.save(systemMember);
        }


//        systemVendorLogInfoRepository.save(log);
    }

    public void delete(Integer id) {
        Optional<UserVisitSystem> optional = ldapUserSystemRepo.findById(id);
        if (optional.isPresent()) {
            UserVisitSystem bean = optional.get();
//            bean.setDeleted(DictConstant.DELETED);

            ldapUserSystemRepo.deleteById(id);
        } else {
            throw PlatformException.errorWithOutMail("删除记录不存在");
        }
    }

//    public List<UserVisitSystem> search(UserVisitSystem params) {
//        params.setDeleted(DictConstant.NO_DELETE);
//        List<UserVisitSystem> result = ldapDepartmentSystemRepo.searchByParam(params);
//        return result;
//    }

//    public List<String> querySystemCodeByMemberAccount(String account) {
//        List<SystemMain> systemMains = ldapDepartmentSystemRepo.querySystemCodeByMemberAccount(account);
//        if (CollectionUtil.isNotEmpty(systemMains)) {
//            return systemMains.stream().map(SystemMain::getSystemClassNo).collect(Collectors.toList());
//        } else {
//            return null;
//        }
//    }


    public Page<UserVisitSystem> page(int pageNo, int pageSize, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "id";

        pageNo = PageUtil.initPageNo(pageNo);
        pageSize = PageUtil.initPageSize(pageSize);

        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, direction, sort);
//        param.put("EQ_deleted", 0);
        Page<UserVisitSystem> page = ldapUserSystemRepo.findAll(SpecificationUtil.buildSpecification(param, UserVisitSystem.class), pageable);

        return page;
    }
}
